/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package ecarx.xsf.notificationcenter;

import android.app.Application;
import android.os.Build;
import android.util.Log;
import android.view.Display;

import com.ecarx.xui.adaptapi.binder.IConnectable;
import com.ecarx.xui.adaptapi.car.Car;
import com.ecarx.xui.adaptapi.car.ICar;
import com.ecarx.xui.adaptapi.car.base.ICarInfo;
import com.ecarx.xui.adaptapi.vehicle.VehicleZone;

import ecarx.xsf.notificationcenter.push.NotificationPushFlavor;
import ecarx.xsf.notificationcenter.utils.NotificationAccess;
import ecarx.xsf.notificationcenter.utils.NotificationBackend;


public class LauncherControllerApp extends Application {

    private static final String TAG = "LauncherControllerApp";
    public static final int DISPLAY_TYPE_CSD = 101;
    public static final int DISPLAY_TYPE_PSD = 102;
    public static final int DISPLAY_TYPE_RSD = 103;
    public static final int DISPLAY_TYPE_RFDM = 104;
    private static Application app;

    private ICar mICar;
    private static Display display2;

    public static Application get() {
        return app;
    }

    public static Display getPsdDisplay() {
        return display2;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LauncherControllerApp.app = this;
        initBlockNotifications();
        NotificationAccess.enableNotificationAccess(getApplicationContext(),
                getApplicationContext().getPackageName(),
                ECarxNotificationListenerService.class.getName());
        initPush();
        initCar();
    }

    /**
     * 初始化 car 模块
     */
    private void initCar() {
        if (mICar == null) {
            mICar = Car.create(this);
            if (mICar instanceof IConnectable) {
                IConnectable mIConnectable = (IConnectable) mICar;
                mIConnectable.connect();
                mIConnectable.registerConnectWatcher(new IConnectable.IConnectWatcher() {
                    @Override
                    public void onConnected() {
                        Log.d(TAG, "onConnected");
                        display2 = getDisplayByType(DISPLAY_TYPE_PSD);
                    }

                    @Override
                    public void onDisConnected() {
                        Log.d(TAG, "onDisConnected");
                    }
                });
            } else {
                Log.d(TAG, "mICar is not Iconnectable");
            }
        }
    }

    /**
     *
     * * 从 adapapi 中获取 Display.
     *
     * @param type adapapi 屏幕类型
     * @return Display
     * @since 1.0.0
     */
    private Display getDisplayByType(int type) {
        Display display = null;
        try {
            ICarInfo mICarInfo = mICar.getCarInfoManager();
            if (mICarInfo != null) {
                switch (type) {
                    case DISPLAY_TYPE_CSD:
                        display = mICarInfo.getPresentationDisplay(VehicleZone.ZONE_ROW_1_LEFT);
                        break;

                    case DISPLAY_TYPE_PSD:
                        display = mICarInfo.getPresentationDisplay(VehicleZone.ZONE_ROW_1_RIGHT);
                        break;

                    case DISPLAY_TYPE_RSD:
                        display = mICarInfo.getPresentationDisplay(VehicleZone.ZONE_ROW_2_ALL);
                        break;

                    case DISPLAY_TYPE_RFDM:
                        display = mICarInfo.getPresentationDisplay(VehicleZone.ZONE_ROOF_TOP);
                        break;

                    default:
                        break;
                }
            }
            return display;

        } catch (Throwable throwable) {
            Log.e(TAG, "[ControlBoardService][getDisplayByType]"
                    + ", throwable: " + Log.getStackTraceString(throwable)
            );
        }
        Log.i(TAG, "[ControlBoardService][getDisplayByType]"
                + ", no psd display ");
        return null;
    }

    private void initPush() {
        NotificationPushFlavor.getPushManager().init(this);
    }

    private void initBlockNotifications() {

        Log.i(TAG, "initBlockNotifications() called");
        final String[] blackPkgArray = getResources().getStringArray(R.array.notify_black_list);

        for (final String pkg : blackPkgArray) {
            boolean result = NotificationBackend.blockNotificationForPackage(this, pkg);
            Log.i(TAG, "block pkg=" + pkg + "with result=" + result);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationBackend.blockNotificationForChannel(this, "com.qualcomm.qti.smq", "QDMAUI_Noti_Channel");
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String packageName = "com.android.systemui";
            NotificationBackend.enableNotificationForPackage(this, packageName, true);
            final String[] blackChannels = getResources().getStringArray(R.array.channel_black_list);
            for (int i = 0; i < 9; i++) {
                NotificationBackend.enableNotificationForChannel(this, packageName, blackChannels[i], false);
            }
        }
    }
}
